package com.chinatgl.latte.net;

import android.content.Context;

import com.chinatgl.latte.app.ConfigKeys;
import com.chinatgl.latte.app.Latte;
import com.chinatgl.latte.net.callback.IError;
import com.chinatgl.latte.net.callback.IFailure;
import com.chinatgl.latte.net.callback.IRequest;
import com.chinatgl.latte.net.callback.ISuccess;
import com.chinatgl.latte.ui.LoaderStyle;

import java.io.File;
import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * 文件描述: 构建网络请求
 * 作者: Created by 杨伟乔 on 2017/8/7 0007.
 * 版本号: 1.0
 */

public class RestClientBuilder {

    private String mUrl = null;
    private WeakHashMap<String, Object> PARAMS = RestCreator.getParams();
    private IRequest mRequest = null;
    private ISuccess mSuccess = null;
    private IError mError = null;
    private IFailure mFailure = null;
    private RequestBody mBody = null;
    private Context mContext = null;
    private LoaderStyle mLoaderStyle = null;
    private File mFile = null;

    private String mDownloadDir = null;
    private String mExtension = null;
    private String mName = null;

    RestClientBuilder() {

    }

    /**
     * url地址
     *
     * @param url
     * @return
     */
    public final RestClientBuilder url(String url) {
        this.mUrl =  url;
        return this;
    }

    /**
     * @param params 传入参数map
     * @return
     */
    public final RestClientBuilder params(WeakHashMap<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    /**
     * 传入原始的参数
     *
     * @param key
     * @param value
     * @return
     */
    public final RestClientBuilder params(String key, Object value) {

        PARAMS.put(key, value);
        return this;
    }

    public final RestClientBuilder raw(String raw) {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }
    //文件直接上传
    public final RestClientBuilder file(File file) {
        this.mFile = file;
        return this;
    }
    //根据地址上传文件
    public final RestClientBuilder file(String file) {
        this.mFile = new File(file);
        return this;
    }

    public final RestClientBuilder name(String name) {
        this.mName = name;
        return this;
    }

    public final RestClientBuilder dir(String dir) {
        this.mDownloadDir = dir;
        return this;
    }

    public final RestClientBuilder extension(String extension) {
        this.mExtension = extension;
        return this;
    }





    /**
     * 成功的回调
     *
     * @param iSuccess
     * @return
     */
    public final RestClientBuilder success(ISuccess iSuccess) {
        this.mSuccess = iSuccess;
        return this;
    }

    /**
     * 失败的回调
     *
     * @param iFailure
     * @return
     */
    public final RestClientBuilder fail(IFailure iFailure) {
        this.mFailure = iFailure;
        return this;
    }

    /**
     * 错误的回调
     *
     * @param iError
     * @return
     */
    public final RestClientBuilder error(IError iError) {
        this.mError = iError;
        return this;
    }

    /**
     * 请求的回调
     *
     * @param iRequest
     * @return
     */
    public final RestClientBuilder onRequest(IRequest iRequest) {
        this.mRequest = iRequest;
        return this;
    }

    public final RestClientBuilder loader(Context context, LoaderStyle loaderStyle) {
        this.mContext = context;
        this.mLoaderStyle = loaderStyle;
        return this;
    }

    public final RestClientBuilder loader(Context context) {
        this.mContext = context;
        this.mLoaderStyle = LoaderStyle.BallClipRotatePulseIndicator;
        return this;
    }

    public final RestClient build() {
        return new RestClient(mUrl, PARAMS, mRequest,mDownloadDir,mExtension,mName, mSuccess, mError, mFailure, mBody,mFile, mLoaderStyle, mContext);
    }
}
